function seekAllDrivers()
{
    var argsArray=new Array();
    argsArray.push(newArg("Action","Seek"));
    sendRequest("../controller/controllerDriver.jsp",argsArray,show);
}

function addDriver()
{
    
    var argsArray=new Array();
    argsArray.push(newArg("Action","Add"));
    var txtID=document.getElementById("newId").value.toUpperCase();
    var name=document.getElementById("nameNew").value.toUpperCase();
    var type=document.getElementById("newType").value.toUpperCase();
    if(txtID != '' && name != ''){
        argsArray.push(newArg("ID",txtID))
        argsArray.push(newArg("Name",name));
        argsArray.push(newArg("Type",type));
        sendRequest("../controller/controllerDriver.jsp",argsArray,btnAdd_onclick_response);
    }else{
        
        jConfirm('Faltan datos por ingresar!!')
    }
}

function btnAdd_onclick_response()
{
    if (isValidResponse(this))
    {
        var res=this.responseText;
        if(res!="")
        {
            var info=eval("("+res+")");            
            if(info.errorcode == 0){
                if(info.data[0].ans == 1){
                    jAlert("Se adiciono el conductor Correctamente");
                }else
                    jConfirm('La identificacion ingresada ya esta registrada.')

            } else
                jConfirm("Error no se pudo registrar el nuevo conductor");
            seekAllDrivers();
        }
    }

}

function radiovalue(radios) {
    for (i = 0; radio = radios[i]; i++) {
        if (radio.checked) {

            return radio.value;
        }
    }
    return -1

}

function updateDriver()
{
    var argsArray=new Array();
    argsArray.push(newArg("Action","Update"));
    var grupo=document.getElementById('ll').group1;
    var values = radiovalue(grupo);
    if(values != -1){
        var txtID=values;
        var name=document.getElementById("nameDriver"+values).value.toUpperCase();
        var type=document.getElementById("type"+values).value.toUpperCase();
        if(txtID != '' && name != ''){
            argsArray.push(newArg("ID",txtID));
            argsArray.push(newArg("Name",name));
            argsArray.push(newArg("Type",type));
            jConfirm('Esta seguro que desea modificar los datos del Conductor?', 'Confirmacion', function(r) {
               if(r){
                    sendRequest("../controller/controllerDriver.jsp",argsArray,btnUpdate_onclick_response);
               }})
               
              
        }else{
           jAlert('Faltan Datos por ingresar','Error', 'Error Dialog');
             
         //  $('#lisDrivers').jAlert('Welcome to jAlert Demo Page', "errboxid");
         //   alert('Faltan Datos por ingresar')
        }
    }else 
        
        jAlert('Debe seleccionar la fila correspondiente a modificar','Error', 'Error Dialog');

}

function btnUpdate_onclick_response()
{
    if (isValidResponse(this))
    {
        var res=this.responseText;
        if(res!="")
        {
            var info=eval("("+res+")");
            if(info.errorcode == 0){
                if(info.data[0].ans == 1){
                    jAlert("Se modifico el conductor Correctamente");
                    seekAllDrivers();
                }
            }else
                jConfirm("Error");
        }
    }

}

function deleteDriver()
{
    var argsArray=new Array();
    argsArray.push(newArg("Action","Delete"));
    var grupo=document.getElementById('ll').group1;
    var values = radiovalue(grupo);
    if(values != -1){
        argsArray.push(newArg("ID",values));
        jConfirm('Esta seguro que desea eliminar los datos del Conductor?', 'Confirmacion', function(r) {
               if(r){
                    sendRequest("../controller/controllerDriver.jsp",argsArray,btnDelete_onclick_response);
               }})
    }else
         jAlert('Debe seleccionar la fila correspondiente a Eliminar','Error', 'Error Dialog');
        
    
}

function btnDelete_onclick_response()
{
    if (isValidResponse(this))
    {
        var res=this.responseText;
        if(res!="")
        {
            var info=eval("("+res+")");
            if(info.errorcode == 0){
                jAlert('Borrado correctamente');
                seekAllDrivers();
            }else
                jConfirm("Error");
        }
    }
}

function show()
{
    var html='<form id="ll"><table><tr><td></td><td align="center">ID</td><td align="center">NOMBRE</td><td align="center">TIPO</td></tr>';
    if (isValidResponse(this))
    {
    
        var res=this.responseText;
        if(res!="")
        {
         
            var info=eval("("+res+")");
         
            for(i=0; i<info.data.length; i++){
                html+='<tr> <td width="20px"><input type="radio" name="group1" id="radio'+info.data[i].idDriver+'" value="'+info.data[i].idDriver+'"></td>'
                html+='<td> <input type="text" id="idDriver'+info.data[i].idDriver+'" value="'+info.data[i].idDriver+'"></td>'
                html+='<td> <input type="text" id="nameDriver'+info.data[i].idDriver+'" value="'+info.data[i].nameDriver+'" onkeypress="return validaNombYApe(event)"></td>'
                html+='<td><SELECT id="type'+info.data[i].idDriver+'"NAME="typeDriver'+info.data[i].typeDriver+'" SIZE=1><OPTION VALUE="'+info.data[i].typeDriver+'">'+info.data[i].typeDriver+'</OPTION><OPTION VALUE="PINCIPAL">PRINCIPAL</OPTION><OPTION VALUE="RESEEVA">RESERVA</OPTION> </SELECT></td>'
            }
            html+='</table>'
            html+='</form>'
            var html2  = '<table><tr><td colspan="3" align="center">CONDUCTOR NUEVO<br><br></td></tr>'
            html2+='<tr><td align="center">IDENTIFICACION</td><td align="center">NOMBRE</td><td align="center">TIPO</td></TR>'
            html2+='<tr><td><input type="text" id="newId" onkeypress="return soloNumeros(event)"></td><td><input type="text" id="nameNew" onkeypress="return validaNombYApe(event)"></td>'
            html2+='<td><SELECT NAME="newType" id= "newType"SIZE=1><OPTION VALUE="PINCIPAL">PRINCIPAL</OPTION><OPTION VALUE="RESEEVA">RESERVA</OPTION> </SELECT></td> </tr></table>'
            document.getElementById('lisDrivers').innerHTML = html
            document.getElementById('new').innerHTML = html2
        }

        else
           jConfirm("Not Found!!");
    }
}

